package com.augurit.common.system.convert;

import java.util.ArrayList;
import java.util.List;

import com.augurit.common.base.extmodel.base.ITreeNode;
import com.augurit.common.system.util.PrivilegeUtils;
import com.augurit.common.system.web.org.form.AcFunctionForm;
import com.augurit.common.system.web.org.form.AcRoleForm;
import com.augurit.common.system.web.org.form.OmUserPrivForm;

public class OmUserPrivConvert {


	/**
	 * 将role转化为OmUserPrivForm对象
	 * @param role
	 * @return
	 */
	public static OmUserPrivForm convertRoleToUserPrivForm(AcRoleForm role){
		if(role != null){
			OmUserPrivForm form = new OmUserPrivForm();
			form.setRoleId(role.getRoleId());
			form.setType(PrivilegeUtils.TYPE_ROLE);
			form.setName(role.getRoleName());
			form.setDesc(role.getRemark());
			form.setIconCls(PrivilegeUtils.CLS_ROLE);
			return form;
		}
		return null;
	}
	
	/**
	 * 将role列表转化为OmUserPrivForm对象列表
	 * @param list role列表
	 * @return OmUserPrivForm对象列表
	 * */
	public static List<OmUserPrivForm> convertRolesToUserPrivForms(List<AcRoleForm> list, boolean isAuthedNode){
		List<OmUserPrivForm> upList = new ArrayList();
		
		if(list != null && list.size() > 0){
			for(AcRoleForm role : list)
				upList.add(convertRoleToUserPrivForm(role));
		}else{
			upList.add(isAuthedNode ? PrivilegeUtils.buildBlankAuthedNode() : PrivilegeUtils.buildBlankInheritNode());
		}

		return upList;
	}
	
	/**
	 * 将function列表转化为OmUserPrivForm对象列表
	 * @param list 列表
	 * @param isPermitted true为特别开通，false为特别禁止
	 * @return OmUserPrivForm对象列表
	 * */
	public  static List<OmUserPrivForm> convertFunsToUserPrivForms(List<AcFunctionForm> list, String privilegeNode){
		List<OmUserPrivForm> result = new ArrayList();
		
		if(list != null && list.size() !=0){
			for(AcFunctionForm func : list)
				result.add(convertFunToUserPrivForm(func));
		}else{
			if(privilegeNode == null)
				result.add(PrivilegeUtils.buildBlankFuncNode());
			else if(privilegeNode.equals(PrivilegeUtils.PERMIT_NODE))
				result.add(PrivilegeUtils.buildBlankPermitNode());
			else if(privilegeNode.equals(PrivilegeUtils.FORBID_NODE))
				result.add(PrivilegeUtils.buildBlankForbidNode());				
		}
		
		return result;
	}
	
	/**
	 * 将function转化为OmUserPrivForm对象
	 * @param func 功能form对象
	 * @return OmUserPrivForm对象
	 * */
	public  static OmUserPrivForm convertFunToUserPrivForm(AcFunctionForm func){
		if(func != null){
			OmUserPrivForm form = new OmUserPrivForm();
			form.setFuncId(func.getFuncId());
			form.setType(PrivilegeUtils.TYPE_FUNC);
			form.setName(func.getFuncName());
			form.setAppName(func.getAppName());
			form.setModuleName(func.getModuleName());
			form.setDesc(func.getFuncDesc());
			form.setMenuNames(func.getMenuNames());
			form.setLeaf(true);
			form.setIconCls(ITreeNode.DEFAULT_LEAF_TREE_NODE_CLS);
			return form;
		}
		return null;
	}
}